﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.Linq;

namespace OpenLightGroup.Common.Converters
{
    /// <summary>
    /// Converts an array of strings to a single comma delimitated string.
    /// </summary>
    public class StringArrayToStringConverter : IValueConverter
    {

        #region IValueConverter Members

        /// <summary>
        /// Converts an array of strings to a single comma delimitated string.
        /// </summary>
        /// <param name="value">An array of strings.</param>
        /// <param name="targetType">The System.Type of data expected by the target dependency property.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the target dependency property.</returns>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            var input = value as IEnumerable<string>;

            if (input != null)
            {
                var sb = new StringBuilder();

                for (int i = 0; i < input.Count(); i++)
                {
                    sb.AppendFormat(culture, "{0}", input.ElementAt(i));

                    if (i < input.Count() - 1)
                    {
                        sb.Append(", ");
                    }

                }

                return sb.ToString();

            }
            else
                return null;
        }

        /// <summary>
        /// Not Implemented
        /// </summary>
        /// <param name="value">The target data being passed to the source.</param>
        /// <param name="targetType">The System.Type of data expected by the source object.</param>
        /// <param name="parameter">An optional parameter to be used in the converter logic.</param>
        /// <param name="culture">The culture of the conversion.</param>
        /// <returns>The value to be passed to the source object.</returns>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
